package org.opengauss.datachecker.extract.data.access;

import lombok.extern.slf4j.Slf4j;
import org.opengauss.datachecker.common.entry.common.DataAccessParam;
import org.opengauss.datachecker.common.entry.enums.ColumnKey;
import org.opengauss.datachecker.common.entry.extract.ColumnsMetaData;
import org.opengauss.datachecker.common.entry.extract.TableMetadata;
import org.opengauss.datachecker.extract.config.DruidDataSourceConfig;
import org.opengauss.datachecker.extract.data.mapper.OracleMetaDataMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;

@Slf4j
@Service
@ConditionalOnBean(DruidDataSourceConfig.class)
@ConditionalOnProperty(prefix = "spring.extract", name = "databaseType", havingValue = "O")
public class OracleDataAccessService extends AbstractDataAccessService{
    @Resource
    private OracleMetaDataMapper oracleMetaDataMapper;

    @Override
    public boolean health() {
        return oracleMetaDataMapper.health();
    }

    @Override
    public List<String> queryTableNameList() {
        return oracleMetaDataMapper.queryTableNameList(properties.getSchema());
    }

    @Override
    public List<TableMetadata> queryTableMetadataList() {
        List<TableMetadata> tableMetadataList = wrapperTableMetadata(oracleMetaDataMapper.queryTableMetadataList(properties.getSchema().toUpperCase(Locale.ROOT)));
        tableMetadataList.forEach(tableMetadata -> {
            tableMetadata.setSchema(properties.getSchema());
        });
        return tableMetadataList;
    }

    @Override
    public List<ColumnsMetaData> queryTableColumnsMetaData(String tableName) {
        log.error(tableName);
        List<ColumnsMetaData> columnsMetaDataList = oracleMetaDataMapper.queryTableColumnsMetaData(properties.getSchema().toUpperCase(Locale.ROOT), tableName.toUpperCase(Locale.ROOT));
        columnsMetaDataList.forEach(columnsMetaData -> {
            if(columnsMetaData.getColumnKey() == ColumnKey.PRI){
                columnsMetaData.setExtra("auto_increment");
            }
        });
        return columnsMetaDataList;
    }

    @Override
    public TableMetadata queryTableMetadata(String tableName) {
        TableMetadata tableMetadata = wrapperTableMetadata(oracleMetaDataMapper.queryTableMetadata(properties.getSchema().toUpperCase(Locale.ROOT), tableName));
        tableMetadata.setSchema(properties.getSchema());
        return tableMetadata;
    }

    @Override
    public long rowCount(String tableName) {
        return oracleMetaDataMapper.rowCount(properties.getSchema(), tableName);
    }

    @Override
    public String min(DataAccessParam param) {
        return oracleMetaDataMapper.min(param);
    }

    @Override
    public String max(DataAccessParam param) {
        return oracleMetaDataMapper.max(param);
    }

    @Override
    public String next(DataAccessParam param) {
        return oracleMetaDataMapper.next(param);
    }
}


